#!/bin/bash

hadoop_home=/naver/pos/hadoop/hadoop-0.18.3/
hadoop_bin=$hadoop_home/bin/hadoop

function upload_log()
{
	date=$1
	if [ ${#date} != 8 ]; then
		echo "$date should be YYYYMMDD"
		exit -1
	fi

   yyyymm=`expr substr $date 1 6`

	# IMP_NA
   tar -zxvf /log_owfs/na_log/${yyyymm}/${date}/imp.na.tar.gz
   chk untar_impna
   $hadoop_bin dfs -rmr /user/kwd_ad/logs/impna/$date
   cat data1/pos/log_process/log/${date}/${date}????/imp.na.${date}???? | perl -MEncode -ne 'binmode STDOUT, ":utf8"; print decode("cp949", $_);' | $hadoop_bin dfs -put - /user/kwd_ad/logs/impna/$date/imp.na
   chk upload_impna
   rm -rf data1/pos/log_process/log/${date}/
   
   # IMP_MEDIA
   tar -zxvf /log_owfs/na_log/${yyyymm}/${date}/imp.adpost.mobile.tar.gz
   chk untar_impmedia_mobile
   $hadoop_bin dfs -rmr /user/kwd_ad/logs/impmedia/$date
   cat data1/pos/log_process/log/${date}/${date}????/imp.blog.mobile.${date}???? | awk '{print $0"\t-1"}' | $hadoop_bin dfs -put - /user/kwd_ad/logs/impmedia/$date/imp.media.mobile
   chk upload_impmedia
#   rm -rf data1/pos/log_process/log/${date}/
	
	# CLK_NA
   tar -zxvf /log_owfs/na_log/${yyyymm}/${date}/clk.na.rev.tar.gz
   chk untar_clkna
   $hadoop_bin dfs -rmr /user/kwd_ad/logs/clknarev/${date}
   cat data1/pos/log_process/log/${date}/${date}/clk.na.rev | perl -MEncode -ne 'binmode STDOUT, ":utf8"; print decode("cp949", $_);' |  $hadoop_bin dfs -put - /user/kwd_ad/logs/clknarev/${date}/clk.na.rev
   chk upload_clkna
   rm -rf data1/pos/log_process/log/${date}/
   
   # CLK_MEDIA
   tar -zxvf /log_owfs/na_log/${yyyymm}/${date}/clk.adpost.rev.tar.gz
   chk untar_clkmedia
   $hadoop_bin dfs -rmr /user/kwd_ad/logs/clkmediarev/${date}
   cat data1/pos/log_process/log/${date}/${date}/clk.blog.rev | perl -MEncode -ne 'binmode STDOUT, ":utf8"; print decode("cp949", $_);' |  $hadoop_bin dfs -put - /user/kwd_ad/logs/clkmediarev/${date}/clk.media.rev
   chk upload_clkmedia
#   rm -rf data1/pos/log_process/log/${date}/
}


function remove_log()
{
	date=$1
	if [ ${#date} != 8 ]; then
		echo "$date should be YYYYMMDD"
		return -1
	fi

   yyyymm=`expr substr $date 1 6`

   $hadoop_bin dfs -rmr /user/kwd_ad/logs/impna/$date
   $hadoop_bin dfs -rmr /user/kwd_ad/logs/impmedia/$date
   $hadoop_bin dfs -rmr /user/kwd_ad/logs/clknarev/${date}
   $hadoop_bin dfs -rmr /user/kwd_ad/logs/clkmediarev/${date}
}
